#include "stm32f10x.h"
#include "epd.h"
#include "delay.h"
#include "Fontlibrary.h"
#include "string.h"
#include "epd.h"

extern const uint8_t Wallpaper[];

extern const uint8_t Weather[];

extern const uint8_t Clock[];

extern const uint8_t Internet[];

extern const uint8_t EPD_Char8x16[][16];
// extern const Chinesecell_t EPD_Chinese16x16[];

const uint8_t LUT_DATA_Full[] = { // 30 bytes
    0x50, 0xAA, 0x55, 0xAA, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xFF, 0xFF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t LUT_DATA_part[] = { // 30 bytes
    0x10, 0x18, 0x18, 0x08, 0x18, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x13, 0x14, 0x44, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t LUT_DATA_16Gray[] = {
    0x00, 0x0A, 0x05, 0x0A, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const uint8_t LUT_DATA_4Gray[] = {
    0x00, 0x0A, 0x05, 0x0A, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

void EPD_WriteByte(uint8_t Byte)
{
    EPD_CS_ENABLE();
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) // 检查接收标志位
    {
    }
    SPI_I2S_SendData(SPI1, Byte);
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) != RESET)
    {
    }
    EPD_CS_DISABLE();
}

void EPD_WriteCMD(uint8_t Command)
{
    EPD_CS_ENABLE();
    EPD_DC_Command();
    EPD_WriteByte(Command);
    EPD_CS_DISABLE();
    // EPD_DC_Data();
}

void EPD_WriteData(uint8_t Data)
{
    EPD_CS_ENABLE();
    EPD_DC_Data();
    EPD_WriteByte(Data);
    EPD_CS_DISABLE();
}

void EPD_READBUSY(void)
{
    while (1)
    {
        if (EPD_Busy() == 1)
        {
            break;
        }
    }
}

void EPD_select_LUT(const uint8_t *wave_data)
{
    uint8_t count;
    EPD_WriteCMD(0x32);
    for (count = 0; count < 30; count++)
        EPD_WriteData(wave_data[count]);
}

void EPD_DataEnterMode(uint8_t em)
{
    if (em == 0x00) // x递减，y递减
    {
        EPD_WriteCMD(0x11); // data entry mode
        EPD_WriteData(0x00);

        EPD_WriteCMD(0x44);  // set Ram-X address start/end position
        EPD_WriteData(0x0F); // 0x0C-->(15+1)*8=128
        EPD_WriteData(0x00);

        EPD_WriteCMD(0x45); // set Ram-Y address start/end position
        EPD_WriteData(0x27);
        EPD_WriteData(0x01);
        EPD_WriteData(0x00); // Y%256--->(295+1)=296
        EPD_WriteData(0x00); // Y%256

        EPD_WriteCMD(0x4E);  // set RAM x address count to 0;
        EPD_WriteData(0x0F); // 0x0C-->(15+1)*8=128
        EPD_WriteCMD(0x4F);  // set RAM y address count to 0X127;
        EPD_WriteData(0x27); // Y%256--->(295+1)=296
        EPD_WriteData(0x01); // Y%256}
    }
    if (em == 0x01) // x递增，y递减
    {
        EPD_WriteCMD(0x11); // data entry mode
        EPD_WriteData(0x01);

        EPD_WriteCMD(0x44); // set Ram-X address start/end position
        EPD_WriteData(0x00);
        EPD_WriteData(0x0f); // 0x0C-->(15+1)*8=128

        EPD_WriteCMD(0x45); // set Ram-Y address start/end position
        EPD_WriteData(0x27);
        EPD_WriteData(0x01);
        EPD_WriteData(0x00); // Y%256--->(295+1)=296
        EPD_WriteData(0x00); // Y%256

        EPD_WriteCMD(0x4E); // set RAM x address count to 0;
        EPD_WriteData(0x00);
        EPD_WriteCMD(0x4F);  // set RAM y address count to 0X127;
        EPD_WriteData(0x27); // Y%256--->(295+1)=296
        EPD_WriteData(0x01); // Y%256}
    }
    if (em == 0x02) // x递减，y递增
    {
        EPD_WriteCMD(0x11); // data entry mode
        EPD_WriteData(0x02);

        EPD_WriteCMD(0x44);  // set Ram-X address start/end position
        EPD_WriteData(0x0f); // 0x0C-->(15+1)*8=128
        EPD_WriteData(0x00);

        EPD_WriteCMD(0x45);  // set Ram-Y address start/end position
        EPD_WriteData(0x00); // Y%256--->(295+1)=296
        EPD_WriteData(0x00); // Y%256
        EPD_WriteData(0x27);
        EPD_WriteData(0x01);

        EPD_WriteCMD(0x4E);  // set RAM x address count to 0;
        EPD_WriteData(0x0f); // 0x0C-->(15+1)*8=128
        EPD_WriteCMD(0x4F);  // set RAM y address count to 0X127;
        EPD_WriteData(0x00); // Y%256--->(295+1)=296
        EPD_WriteData(0x00); // Y%256
    }
    if (em == 0x03) // x递增，y递增
    {
        EPD_WriteCMD(0x11); // data entry mode
        EPD_WriteData(0x02);

        EPD_WriteCMD(0x44); // set Ram-X address start/end position
        EPD_WriteData(0x00);
        EPD_WriteData(0x0f); // 0x0C-->(15+1)*8=128

        EPD_WriteCMD(0x45);  // set Ram-Y address start/end position
        EPD_WriteData(0x00); // Y%256--->(295+1)=296
        EPD_WriteData(0x00); // Y%256
        EPD_WriteData(0x27);
        EPD_WriteData(0x01);

        EPD_WriteCMD(0x4E); // set RAM x address count to 0;
        EPD_WriteData(0x00);
        EPD_WriteCMD(0x4F);  // set RAM y address count to 0X127;
        EPD_WriteData(0x00); // Y%256--->(295+1)=296
        EPD_WriteData(0x00); // Y%256
    }
}
void EPD_Init(void)
{
    EPD_RES_ENABLE();
    Delay(15);
    EPD_RES_DISABLE();
    Delay(15);

    EPD_WriteCMD(0x01);  // Driver output control
    EPD_WriteData(0x27); // Y%256--->(295+1)=296
    EPD_WriteData(0x01); // Y%256
    EPD_WriteData(0x00);

    EPD_WriteCMD(0x0C);  // softstart
    EPD_WriteData(0xD7); // is necessary
    EPD_WriteData(0xD6); // is necessary
    EPD_WriteData(0x9D); // is necessary

    EPD_WriteCMD(0x2C); // VCOM Voltage
    EPD_WriteData(0x9A);

    EPD_WriteCMD(0x3A); // Dummy Line
    EPD_WriteData(0x1A);
    EPD_WriteCMD(0x3B); // Gate time
    EPD_WriteData(0X08);

    EPD_WriteCMD(0x11); // data entry mode
    EPD_WriteData(0x01);

    EPD_WriteCMD(0x3C); // BorderWavefrom
    EPD_WriteData(0x33);

    EPD_WriteCMD(0x44); // set Ram-X address start/end position
    EPD_WriteData(0x00);
    EPD_WriteData(0x0f); // 0x0C-->(15+1)*8=128

    EPD_WriteCMD(0x45); // set Ram-Y address start/end position
    EPD_WriteData(0x27);
    EPD_WriteData(0x01);
    EPD_WriteData(0x00); // Y%256--->(295+1)=296
    EPD_WriteData(0x00); // Y%256

    EPD_WriteCMD(0x4E); // set RAM x address count to 0;
    EPD_WriteData(0x00);
    EPD_WriteCMD(0x4F);  // set RAM y address count to 0X127;
    EPD_WriteData(0x27); // Y%256--->(295+1)=296
    EPD_WriteData(0x01); // Y%256}

    EPD_READBUSY();
    EPD_select_LUT(LUT_DATA_Full);
}

void EPD_Part_Init(void)
{
    EPD_Init(); // Electronic paper initialization
    EPD_select_LUT(LUT_DATA_part);
    EPD_POWER_ON();
    EPD_WriteCMD(0x3C); // BorderWavefrom
    EPD_WriteData(0x01);
    Delay(80);
}

void EPD_Full_Init(void)
{
    EPD_Init(); // Electronic paper initialization
    EPD_select_LUT(LUT_DATA_Full);
}

void EPD_POWER_ON(void)
{
    EPD_WriteCMD(0x22); // Display Update Control
    EPD_WriteData(0xC0);
    EPD_WriteCMD(0x20); // Activate Display Update Sequence
    EPD_READBUSY();
}

void EPD_POWER_OFF(void)
{
    EPD_WriteCMD(0x22); // Display Update Control
    EPD_WriteData(0xC3);
    EPD_WriteCMD(0x20); // Activate Display Update Sequence
    EPD_READBUSY();
}

void EPD_DeepSleep(void)
{
    EPD_WriteCMD(0x10); // enter deep sleep
    EPD_WriteData(0x01);
    Delay(100);
}

void EPD_Update_Full(void)
{
    EPD_WriteCMD(0x22); // Display Update Control
    EPD_WriteData(0xC4);
    EPD_WriteCMD(0x20); // Activate Display Update Sequence
    EPD_READBUSY();
}

void EPD_Update_Part(void)
{
    EPD_WriteCMD(0x22);
    EPD_WriteData(0x04); // different
    EPD_WriteCMD(0x20);
    EPD_READBUSY();
}

void EPD_BlackScreen(void)
{
    EPD_Full_Init();
    uint32_t i, k;
    EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
    for (k = 0; k < 296; k++)
    {
        for (i = 0; i < 16; i++)
        {
            EPD_WriteData(0x00);
        }
    }
    EPD_Update_Full();
}
void EPD_WhiteScreen(void)
{
    EPD_Full_Init();
    uint32_t i, k;
    EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
    for (k = 0; k < 296; k++)
    {
        for (i = 0; i < 16; i++)
        {
            EPD_WriteData(0xff);
        }
    }
    EPD_Update_Full();
}

void EPD_WBScreen(void)
{
    EPD_Full_Init();
    uint32_t i, k;
    EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
    for (k = 0; k < 148; k++)
    {
        for (i = 0; i < 16; i++)
        {
            EPD_WriteData(0x00);
        }
    }
    for (k = 0; k < 148; k++)
    {
        for (i = 0; i < 16; i++)
        {
            EPD_WriteData(0xff);
        }
    }
    EPD_Update_Full();
}

void EPD_ShowBMP(const uint8_t *data)
{
    EPD_Full_Init();
    uint32_t i;
    EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
    for (i = 0; i < 4736; i++)
    {
        EPD_WriteData(data[i]);
    }
    EPD_Update_Full();
}

void EPD_4Gray_Init(void)
{
    EPD_Init();
    EPD_select_LUT(LUT_DATA_4Gray);
    Delay(500);
}

void EPD_16Gray_Init(void)
{
    EPD_Init();
    EPD_select_LUT(LUT_DATA_16Gray);
    Delay(500);
}

void EPD_16Graytest(void)
{
    EPD_16Gray_Init();
    uint32_t j;
    for (j = 0; j < 16; j++)
    {
        uint32_t i, k;
        EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
        for (k = 0; k < 296 - (18 * (j + 1)); k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0x00);
            }
        }

        for (k = 0; k < 18 * (j + 1); k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0xff);
            }
        }
        EPD_Update_Full();
        Delay(1000);
    }
}

void EPD_4Graytest(void)
{
    EPD_4Gray_Init();
    uint32_t j;
    for (j = 0; j < 4; j++)
    {
        uint32_t i, k;

        EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
        for (k = 0; k < 296 - 74 * (j + 1); k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0x00);
            }
        }

        for (k = 0; k < 74 * (j + 1); k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0xff);
            }
        }
        EPD_Update_Full();
        Delay(1000);
    }
}

void EPD_8Graytest(void)
{
    EPD_16Gray_Init();
    uint32_t j;
    for (j = 0; j < 2; j++)
    {
        uint32_t i, k;
        EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
        for (k = 0; k < 259; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0x00);
            }
        }

        for (k = 0; k < 37; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0xff);
            }
        }
        EPD_Update_Full();
        Delay(1000);
    }
    for (j = 0; j < 2; j++)
    {
        uint32_t i, k;
        EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
        for (k = 0; k < 222; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0x00);
            }
        }

        for (k = 0; k < 74; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0xff);
            }
        }
        EPD_Update_Full();
        Delay(1000);
    }
    for (j = 0; j < 2; j++)
    {
        uint32_t i, k;
        EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
        for (k = 0; k < 185; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0x00);
            }
        }

        for (k = 0; k < 111; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0xff);
            }
        }
        EPD_Update_Full();
        Delay(1000);
    }
    for (j = 0; j < 2; j++)
    {
        uint32_t i, k;
        EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
        for (k = 0; k < 148; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0x00);
            }
        }

        for (k = 0; k < 148; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0xff);
            }
        }
        EPD_Update_Full();
        Delay(1000);
    }
    for (j = 0; j < 2; j++)
    {
        uint32_t i, k;
        EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
        for (k = 0; k < 111; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0x00);
            }
        }

        for (k = 0; k < 185; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0xff);
            }
        }
        EPD_Update_Full();
        Delay(1000);
    }
    for (j = 0; j < 2; j++)
    {
        uint32_t i, k;
        EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
        for (k = 0; k < 74; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0x00);
            }
        }

        for (k = 0; k < 222; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0xff);
            }
        }
        EPD_Update_Full();
        Delay(1000);
    }
    for (j = 0; j < 2; j++)
    {
        uint32_t i, k;
        EPD_WriteCMD(0x24); // write RAM for black(0)/white (1)
        for (k = 0; k < 37; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0x00);
            }
        }

        for (k = 0; k < 259; k++)
        {
            for (i = 0; i < 16; i++)
            {
                EPD_WriteData(0xff);
            }
        }
        EPD_Update_Full();
        Delay(1000);
    }
}

void EPD_Dis_Part(uint32_t x_start, uint32_t y_start, const uint8_t *datas, uint32_t PART_COLUMN, uint32_t PART_LINE)
{
    EPD_Part_Init();
    const uint8_t *dataflag;
    uint32_t i;
    uint32_t x_end, y_start1, y_start2, y_end1, y_end2;
    dataflag = datas;
    // datas_flag = datas;    // Record array first address
    x_start = x_start / 8; // Convert to byte
    x_end = x_start + PART_LINE / 8 - 1;

    y_start1 = 0;
    y_start2 = y_start - 1;
    if (y_start >= 256)
    {
        y_start1 = y_start2 / 256;
        y_start2 = y_start2 % 256;
    }
    y_end1 = 0;
    y_end2 = y_start + PART_COLUMN - 1;
    if (y_end2 >= 256)
    {
        y_end1 = y_end2 / 256;
        y_end2 = y_end2 % 256;
    }

    EPD_WriteCMD(0x44);      // set RAM x address start/end, in page 35
    EPD_WriteData(x_start);  // RAM x address start at 00h;
    EPD_WriteData(x_end);    // RAM x address end at 0fh(15+1)*8->128
    EPD_WriteCMD(0x45);      // set RAM y address start/end, in page 35
    EPD_WriteData(y_start2); // RAM y address start at 0127h;
    EPD_WriteData(y_start1); // RAM y address start at 0127h;
    EPD_WriteData(y_end2);   // RAM y address end at 00h;
    EPD_WriteData(y_end1);   // ????=0

    EPD_WriteCMD(0x4E); // set RAM x address count to 0;
    EPD_WriteData(x_start);
    EPD_WriteCMD(0x4F); // set RAM y address count to 0X127;
    EPD_WriteData(y_start2);
    EPD_WriteData(y_start1);

    EPD_WriteCMD(0x24); // Write Black and White image to RAM
    for (i = 0; i < PART_COLUMN * PART_LINE / 8; i++)
    {
        EPD_WriteData(*datas);
        datas++;
    }
    EPD_Update_Part();

    datas = dataflag;

    EPD_WriteCMD(0x44);      // set RAM x address start/end, in page 35
    EPD_WriteData(x_start);  // RAM x address start at 00h;
    EPD_WriteData(x_end);    // RAM x address end at 0fh(15+1)*8->128
    EPD_WriteCMD(0x45);      // set RAM y address start/end, in page 35
    EPD_WriteData(y_start2); // RAM y address start at 0127h;
    EPD_WriteData(y_start1); // RAM y address start at 0127h;
    EPD_WriteData(y_end2);   // RAM y address end at 00h;
    EPD_WriteData(y_end1);   // ????=0

    EPD_WriteCMD(0x4E); // set RAM x address count to 0;
    EPD_WriteData(x_start);
    EPD_WriteCMD(0x4F); // set RAM y address count to 0X127;
    EPD_WriteData(y_start2);
    EPD_WriteData(y_start1);

    EPD_WriteCMD(0x24); // Write Black and White image to RAM
    for (i = 0; i < PART_COLUMN * PART_LINE / 8; i++)
    {
        EPD_WriteData(*datas);
        datas++;
    }
}

void EPD_ShowChar(uint8_t line, uint8_t column, char Char)
{
    // line最大8，column最大37
    uint32_t x_start = 0;
    x_start = (line - 1) * 16;
    uint32_t y_start = 296;
    y_start = 296 - (column - 1) * 8;
    uint8_t PART_COLUMN = 8,
            PART_LINE = 16;
    EPD_Part_Init();
    uint32_t i;
    uint32_t x_end, y_start1, y_start2, y_end1, y_end2;
    x_start = x_start / 8; // Convert to byte
    x_end = x_start + PART_LINE / 8 - 1;

    y_start1 = 0;
    y_start2 = y_start - 1;
    if (y_start >= 256)
    {
        y_start1 = y_start2 / 256;
        y_start2 = y_start2 % 256;
    }
    y_end1 = 0;
    y_end2 = y_start + PART_COLUMN - 1;
    if (y_end2 >= 256)
    {
        y_end1 = y_end2 / 256;
        y_end2 = y_end2 % 256;
    }

    EPD_WriteCMD(0x44);      // set RAM x address start/end, in page 35
    EPD_WriteData(x_start);  // RAM x address start at 00h;
    EPD_WriteData(x_end);    // RAM x address end at 0fh(15+1)*8->128
    EPD_WriteCMD(0x45);      // set RAM y address start/end, in page 35
    EPD_WriteData(y_start2); // RAM y address start at 0127h;
    EPD_WriteData(y_start1); // RAM y address start at 0127h;
    EPD_WriteData(y_end2);   // RAM y address end at 00h;
    EPD_WriteData(y_end1);   // ????=0

    EPD_WriteCMD(0x4E); // set RAM x address count to 0;
    EPD_WriteData(x_start);
    EPD_WriteCMD(0x4F); // set RAM y address count to 0X127;
    EPD_WriteData(y_start2);
    EPD_WriteData(y_start1);

    EPD_WriteCMD(0x24); // Write Black and White image to RAM
    for (i = 0; i < PART_COLUMN * PART_LINE / 8; i++)
    {
        EPD_WriteData(EPD_Char8x16[Char - ' '][i]);
    }
    EPD_Update_Part();
}

void EPD_ShowString(uint8_t line, uint8_t column, char *String)
{
    uint8_t i;
    for (i = 0; String[i] != '\0'; i++)
    {
        if (column > 37)
        {
            column = 1;
            line++;
            EPD_ShowChar(line, column, String[i]);
            column++;
            continue;
        }
        EPD_ShowChar(line, column, String[i]);
        column++;
    }
}

// void EPD_PartChinesePicture(uint8_t line, uint8_t column, const uint8_t *datas)
// {
//     // line最大8，column最大18
//     uint32_t x_start = 0;
//     x_start = (line - 1) * 16;
//     uint32_t y_start = 296;
//     y_start = 296 - (column - 1) * 16 - 4;
//     uint8_t PART_COLUMN = 16,
//             PART_LINE = 16;
//     EPD_Part_Init();
//     uint32_t i;
//     uint32_t x_end, y_start1, y_start2, y_end1, y_end2;
//     x_start = x_start / 8; // Convert to byte
//     x_end = x_start + PART_LINE / 8 - 1;

//     y_start1 = 0;
//     y_start2 = y_start - 1;
//     if (y_start >= 256)
//     {
//         y_start1 = y_start2 / 256;
//         y_start2 = y_start2 % 256;
//     }
//     y_end1 = 0;
//     y_end2 = y_start + PART_COLUMN - 1;
//     if (y_end2 >= 256)
//     {
//         y_end1 = y_end2 / 256;
//         y_end2 = y_end2 % 256;
//     }

//     EPD_WriteCMD(0x44);      // set RAM x address start/end, in page 35
//     EPD_WriteData(x_start);  // RAM x address start at 00h;
//     EPD_WriteData(x_end);    // RAM x address end at 0fh(15+1)*8->128
//     EPD_WriteCMD(0x45);      // set RAM y address start/end, in page 35
//     EPD_WriteData(y_start2); // RAM y address start at 0127h;
//     EPD_WriteData(y_start1); // RAM y address start at 0127h;
//     EPD_WriteData(y_end2);   // RAM y address end at 00h;
//     EPD_WriteData(y_end1);   // ????=0

//     EPD_WriteCMD(0x4E); // set RAM x address count to 0;
//     EPD_WriteData(x_start);
//     EPD_WriteCMD(0x4F); // set RAM y address count to 0X127;
//     EPD_WriteData(y_start2);
//     EPD_WriteData(y_start1);

//     EPD_WriteCMD(0x24); // Write Black and White image to RAM
//     for (i = 0; i < PART_COLUMN * PART_LINE / 8; i++)
//     {
//         EPD_WriteData(datas[i]);
//     }
//     EPD_Update_Part();
// }

// void EPD_PartChinese(uint8_t line, uint8_t column, uint8_t PIndex)
// {
//     // line最大8，column最大18
//     uint32_t x_start = 0;
//     x_start = (line - 1) * 16;
//     uint32_t y_start = 296;
//     y_start = 296 - (column - 1) * 16 - 4;
//     uint8_t PART_COLUMN = 16,
//             PART_LINE = 16;
//     EPD_Part_Init();
//     uint32_t i;
//     uint32_t x_end, y_start1, y_start2, y_end1, y_end2;
//     x_start = x_start / 8; // Convert to byte
//     x_end = x_start + PART_LINE / 8 - 1;

//     y_start1 = 0;
//     y_start2 = y_start - 1;
//     if (y_start >= 256)
//     {
//         y_start1 = y_start2 / 256;
//         y_start2 = y_start2 % 256;
//     }
//     y_end1 = 0;
//     y_end2 = y_start + PART_COLUMN - 1;
//     if (y_end2 >= 256)
//     {
//         y_end1 = y_end2 / 256;
//         y_end2 = y_end2 % 256;
//     }

//     EPD_WriteCMD(0x44);      // set RAM x address start/end, in page 35
//     EPD_WriteData(x_start);  // RAM x address start at 00h;
//     EPD_WriteData(x_end);    // RAM x address end at 0fh(15+1)*8->128
//     EPD_WriteCMD(0x45);      // set RAM y address start/end, in page 35
//     EPD_WriteData(y_start2); // RAM y address start at 0127h;
//     EPD_WriteData(y_start1); // RAM y address start at 0127h;
//     EPD_WriteData(y_end2);   // RAM y address end at 00h;
//     EPD_WriteData(y_end1);   // ????=0

//     EPD_WriteCMD(0x4E); // set RAM x address count to 0;
//     EPD_WriteData(x_start);
//     EPD_WriteCMD(0x4F); // set RAM y address count to 0X127;
//     EPD_WriteData(y_start2);
//     EPD_WriteData(y_start1);

//     EPD_WriteCMD(0x24); // Write Black and White image to RAM

//     const uint8_t *data;
//     data = EPD_Chinese16x16[PIndex].data;

//     for (i = 0; i < PART_COLUMN * PART_LINE / 8; i++)
//     {

//         EPD_WriteData(data[i]);
//     }
//     EPD_Update_Part();
// }

// void EPD_ShowChinese(uint8_t line, uint8_t column, char *Chinese)
// {
//     char SigleChinese[4] = {0};
//     uint8_t pChinese = 0;
//     uint8_t pIndex;
//     for (uint8_t i = 0; Chinese[i] != '\0'; i++)
//     {
//         SigleChinese[pChinese] = Chinese[i];
//         pChinese++;
//         if (pChinese >= 3)
//         {
//             pChinese = 0;
//             for (pIndex = 0; strcmp(EPD_Chinese16x16[pIndex].Index, "") != 0; pIndex++)
//             {
//                 if (strcmp(EPD_Chinese16x16[pIndex].Index, SigleChinese) == 0)
//                 {
//                     break;
//                 }
//             }
//             if (column > 18)
//             {
//                 column = 1;
//                 line++;
//                 EPD_PartChinese(line, column, pIndex);
//                 column++;
//                 continue;
//             }
//             EPD_PartChinese(line, column, pIndex);
//             column++;
//         }
//     }
// }
